package com.kai.module_cartoon.api


import androidx.lifecycle.LiveData
import com.kai.lib_app.utlis.JsoupUtlis
import com.kai.module_cartoon.cart.CartoonBean
import com.kai.module_cartoon.widget.HorizonBean
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jsoup.Connection
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element
import org.jsoup.select.Elements

/**
 * @Author: 小强同学
 * @Date:  2021/3/22 11:28
 * @Description:  首页获取数据
 */
class APIHomePage {

    /**
     * 获取头部数据
     * */
    suspend fun getHeadHtml() = withContext(Dispatchers.IO) {
        var list: MutableList<CartoonBean.HeadlineBean> = ArrayList()//头部数据
        val navBar: Elements = getDocument().getElementsByClass("navBar") //获取navBar标签的元素
        val container = navBar.select("div.container")
        val a = container.select("a")
        for (label in a) {
            if (label.text() != "首页") {
                list.add(CartoonBean.HeadlineBean(label.text(), label.attr("href")))
            }
        }
        list
    }

    /**
     * 获取Banner数据
     * */
    suspend fun getBannerData() = withContext(Dispatchers.IO) {
        val bannerForm: Elements = getDocument().getElementsByClass("bannerForm ib")
        val ib = bannerForm.select("li.ib")
        var list2: MutableList<CartoonBean.BannerData> = ArrayList()//头部数据
        for (label in ib) {
            var a = label.select("a").attr("href")
            var b = label.select("a > img")
            var src = b.attr("src")
            var title = b.attr("title")
            list2.add(CartoonBean.BannerData(a, src, title))

        }
        list2
    }

    /**
     * 获取漫画精选数据
     * */
    suspend fun getSelected() = withContext(Dispatchers.IO) {
        getData("original", 0)
    }

    /**
     * 获取今日热门数据
     * */
    suspend fun getHot() = withContext(Dispatchers.IO) {
        getData("series",1)
    }

    /**
     * 获取最近更新数据
     * */
    suspend fun getUpdate() = withContext(Dispatchers.IO) {
        getData("series",2)
    }

    /**
     * 获取最近上架数据
     * */
    suspend fun getLately() = withContext(Dispatchers.IO) {
        getData("series",3)
    }

    private fun getData(modular: String, type: Int): MutableList<HorizonBean> {
        var list: MutableList<HorizonBean> = ArrayList()
        val original: Elements = getDocument().getElementsByClass(modular)
        return  when (type) {
            0 -> {
                getListData(original[0],list)
            }
            else -> {
                getTypeListData(type,original)
            }
        }
    }
   private fun getListData(original: Element,list: MutableList<HorizonBean>):MutableList<HorizonBean>{
       val book1: Elements = original.getElementsByClass("bookList_1")
       var bookList = book1[0].getElementsByClass("item ib")
       for (book in bookList) {
           val bookBak = book.getElementsByClass("book")
           val a = bookBak.select("a").attr("href")
           val cover = book.getElementsByClass("cover").attr("src")
           val msg = book.getElementsByClass("msg op").text()
           val title = book.getElementsByClass("title").select("a").text()
           val tip = book.getElementsByClass("tip").select("a").text()
           list.add(HorizonBean(a, cover, msg, tip, title))
       }
       return list
   }

    private fun getTypeListData(type: Int,original: Elements):MutableList<HorizonBean>{
        var   list: MutableList<HorizonBean> = ArrayList()
        for (a in original) {
            var title = a.getElementsByClass("logo ib")
            if (title!=null) {
                var alt = title.select("img").attr("alt")
                if (alt == getType(type)) {
                    list = getListData(a, list)
                }
            }
        }
        return list
    }

    private fun getType(type: Int): String {
        var title = ""
        when (type) {
            1 -> {
                title = "今日热门"
            }
            2 -> {
                title = "最近更新"
            }
            3 -> {
                title = "最新上架"
            }
        }
        return title
    }


    companion object {
        private var mAPIHomePage: APIHomePage? = null
        const val url = "http://www.qiman6.com/"
        fun getInstance(): APIHomePage {
            if (mAPIHomePage == null) {
                synchronized(JsoupUtlis::class.java) {
                    if (mAPIHomePage == null) {
                        mAPIHomePage = APIHomePage()
                    }
                }
            }
            return mAPIHomePage!!
        }

        var connect: Connection? = null
        private var document: Document? = null
        fun getDocument(): Document {
            if (connect == null) {
                connect = Jsoup.connect(url)//获取连接对象
            }
            return if (document == null) {
                document = connect!!.get()//获取url页面的内容并解析成document对象
                document!!
            } else {
                document!!
            }
        }
    }
}